/*****************************************************************************
 *  TouchMe
 *  Copyright (C) 2010  Daniel Lerch Hostalot <dlerch@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *****************************************************************************/


#include "common.h"


// {{{ get_cursor_position()
void get_cursor_position(Display *display, int *x, int *y)
{
	Window inwin;
	Window inchildwin; 
	int rootx, rooty; 
	int childx, childy;  
	Atom atom_type_prop; 
	int actual_format;
	unsigned int mask;
	unsigned long n_items, bytes_after_ret;
	Window *props; 

	(void)XGetWindowProperty(display, DefaultRootWindow(display),
		XInternAtom(display, "_NET_ACTIVE_WINDOW", True), 0, 1, False, 
		AnyPropertyType, &atom_type_prop, &actual_format,
		&n_items, &bytes_after_ret, (unsigned char**)&props);

	XQueryPointer(display, props[0], &inwin,  &inchildwin, &rootx, &rooty, 
			&childx, &childy, &mask);

	*x = childx;
	*y = childy;

	XFree(props);           
}
// }}}

// {{{ get_screen_size()
void get_screen_size(Display *display, int *width, int *height)
{
	int num_sizes;
	Rotation original_rotation;

	Window root = RootWindow(display, 0);
	XRRScreenSize *xrrs = XRRSizes(display, 0, &num_sizes);

	XRRScreenConfiguration *conf = XRRGetScreenInfo(display, root);
	SizeID original_size_id = 
		XRRConfigCurrentConfiguration(conf, &original_rotation);

	*width=xrrs[original_size_id].width;
	*height=xrrs[original_size_id].height;
}
// }}}

// {{{ mouse_move()
void mouse_move(Display *display, int x, int y)
{
	XTestFakeMotionEvent(display, -1, x, y, CurrentTime);
	XFlush(display);
}
// }}}

// {{{ mouse_button_press()
void mouse_button_press(Display *display, int button)
{
	XTestFakeButtonEvent(display, button, True, CurrentTime);
	XFlush(display);
}
// }}}

// {{{ mouse_button_release()
void mouse_button_release(Display *display, int button)
{
	XTestFakeButtonEvent(display, button, False, CurrentTime);
	XFlush(display);
}
// }}}

// {{{ enter_key()
void enter_key(Display* display)
{
	int keycode = XKeysymToKeycode(display, XStringToKeysym("KP_Enter"));
	XTestFakeKeyEvent(display, keycode, True, CurrentTime);
	XTestFakeKeyEvent(display, keycode, False, CurrentTime);
	XFlush(display);
}
// }}}







